$(function() {
	var JieDuanGrid = $("#JieDuanGrid");
	var ZhangJieGrid = $("#ZhangJieGrid");
	var jieduanGridAction = $("#jieduanGridAction");
	// 初始化
	JieDuanGrid.datagrid({
		url : 'phase/list',
		fit : true,
		border : false,
		rownumbers : true,
		pagination : true, // 是否显示分页栏
		columns : [ [
				{
					field : 'phase_name',
					title : '阶段名称',
					align : 'center',
					width : 100
				},
				{
					field : 'add_time',
					title : '阶段添加时间',
					sortable : true,
					align : 'center',
					width : 150,
					formatter : function(value, row, index) {
						var c = new Date(value);// 传入一个时间戳
						c.toLocaleString();// 返回本地时间
						var d = new Date(c);
						var formatIt = d.getFullYear() + '-'
								+ (d.getMonth() + 1) + '-' + d.getDate() + ' '
								+ d.getHours() + ':' + d.getMinutes() + ':'
								+ d.getSeconds();
						return formatIt;

					}
				},
				{
					field : 'user_name',
					title : '添加的老师',
					sortable : true,
					align : 'center',
					width : 100,
					formatter : function(value, row, index) {
						if (value == null) {
							return "不清楚";
						}
						return value;
					}
				},
				{
					field : 'ac',
					title : '操作',
					align : 'center',
					width : 150,
					formatter : function(value, row, index) {
					/*	var bts = [];
						bts.push('<a data-index="' + index
								+ '" class="ctr edit">编辑</a>');
						bts.push('<a data-id="' + row.id + '" data-index="'
								+ index + '" class="ctr delete">删除</a>');
						return bts.join('');*/
						return jieduanGridAction.children("a.ctr").attr('data-id',
								row.id).attr('data-index', index)
								.end().html();
					}
				} ] ],
		toolbar :"#jieduanGridToolbar"
	});

	var phaseName = $("#phaseName");
	// 给检索按钮添加事件
	$("#phaseSearch").on('click', function() {
		JieDuanGrid.datagrid("load", {
			phaseName : "%" + phaseName.val() + "%"
		});
	});
	// 给重置按钮添加事件
	var phaseForm = $("#PhaseSearchForm");
	$("#phaseReset").on('click', function() {
		phaseForm.form('clear');
		JieDuanGrid.datagrid("load", {})
	});

	var gridPanel = JieDuanGrid.datagrid("getPanel");
	gridPanel.on("click", "a.edit", function() {
		var index = this.dataset.index;
		var rows = JieDuanGrid.datagrid("getRows");
		var id = rows[index].phase_id;
		addJieDuanForm(id);
	}).on("click", "a.delete", function() {
		var index = this.dataset.index;
		var rows = JieDuanGrid.datagrid("getRows");
		var id = rows[index].phase_id;
		$.messager.confirm("提示", "你确定要删除此数据？", function(r) {
			if (r) {
				$.get("phase/delete/" + id, function(rs) {
					if (rs.success) {
						JieDuanGrid.datagrid("reload");
					} else {
						$.message.alert("提示", rs.message);
					}
				});
			}
		});
	}).on("click", "a.create", function() {
		addJieDuanForm();
	})

	function addJieDuanForm(id) {
		var dialog = $("<div/>").dialog(
				{
					href : 'jiaoxue/jx-add-jieduan' + (id ? ("/" + id) : ""),
					title : '阶段',
					height : '250',
					width : '340',
					modal : true,
					onClose : function() {
						// 窗口关闭的同时销毁此窗口
						$(this).dialog("destroy");
					},
					buttons : [
							{
								iconCls : 'icon-save',
								text : '保存',
								handler : function() {
									// 获取表单的内容
									var addJieDuanForm = $("#addJieDuanForm");
									if (addJieDuanForm.form('validate')) {
										$.post("phase/save", addJieDuanForm
												.serialize(), function(rs) {
											if (rs.success) {
												JieDuanGrid.datagrid("reload");
												ZhangJieGrid.datagrid("load", {})
												dialog.dialog("close");
											} else {
												$.messager.alert("提示",
														rs.message)
											}
										})
									}
								}
							}, {
								iconCls : 'icon-clear',
								text : '取消',
								handler : function() {
									dialog.dialog("close");
								}
							} ]
				});
	}
});